import { _decorator, Button, Label, Node } from 'cc';
import { BaseWindow } from '../../../core/runtime/UIFramework';

const { ccclass, property } = _decorator;

/**
 * 确认对话框组件
 */
@ccclass('ConfirmDialog')
export class ConfirmDialog extends BaseWindow {
    @property(Label)
    private titleLab: Label | null = null;

    @property(Label)
    private contentLab: Label | null = null;

    @property(Button)
    private okBtn: Button | null = null;

    @property(Button)
    private noBtn: Button | null = null;

    @property(Button)
    private cancelBtn: Button | null = null;

    private _okCallback: (() => void) | null = null;
    private _noCallback: (() => void) | null = null;
    private _cancelCallback: (() => void) | null = null;

    init(): void {
        this._uiName = "ConfirmDialog";
    }

    onOpen(data?: any): void {
        super.onOpen(data);
        
        if (data) {
            if (this.titleLab) {
                this.titleLab.string = data.title || "确认";
            }
            
            if (this.contentLab) {
                this.contentLab.string = data.content || "";
            }
            
            this._okCallback = data.onOk || null;
            this._noCallback = data.onNo || null;
            this._cancelCallback = data.onCancel || null;
            
            // 设置按钮显示
            if (this.noBtn) {
                this.noBtn.node.active = data.showNo !== false;
            }
            
            if (this.cancelBtn) {
                this.cancelBtn.node.active = data.showCancel !== false;
            }
        }
    }

    onClose(): void {
        super.onClose();
        this._okCallback = null;
        this._noCallback = null;
        this._cancelCallback = null;
    }

    /**
     * 确定按钮点击事件
     */
    private onOkBtnClick(): void {
        if (this._okCallback) {
            this._okCallback();
        }
        this.close();
    }

    /**
     * 否定按钮点击事件
     */
    private onNoBtnClick(): void {
        if (this._noCallback) {
            this._noCallback();
        }
        this.close();
    }

    /**
     * 取消按钮点击事件
     */
    private onCancelBtnClick(): void {
        if (this._cancelCallback) {
            this._cancelCallback();
        }
        this.close();
    }
}